/*******************************************************************************
	- Regression Analysis
*******************************************************************************/

********************************************************************************
* initial settings
********************************************************************************

version 14.2
clear 
drop _all
set more off

*----------------------------------

// set your own local path where you put the JAERE_Submission folder
if c(username) == "zhenx" {
	global LOCALPATH "C:/Users/zhenx/Dropbox/Research/Temperature and Athletes/temp_acclimatization/Code" 
}
global PATH "$LOCALPATH/JAERE_Submission"

********************************************************************************
* data preparation
********************************************************************************

*** load data set 
	use "$PATH/Data/TF_Data.dta", clear

*** define weather variables
	global weather_control wind Meet_preciptotal Meet_ozone travel_dist 

*** fixed effects
	global absorb_var athlete season_week event_code##(venue home1) year_code i.Meet_dew_bin

*** drop training temperature lower than 40F to mitigate noise
	/*avoid the case where athletes might get trained indoors during extreme cold weathers*/
	drop if Team_tavg <= 40 

*** drop the observations that have missing values on key variables
	drop if Meet_tavg == . | Team_tavg == . | Meet_preciptotal ==. | Meet_ozone == .

*** set clustered standard error
	global cluster_se meet_code athlete

*** set results folder
	cd "$PATH/Results/Regressions/Baseline"

********************************************************************************
* Table A1: classification of track and field events
********************************************************************************

foreach event in Endurance Sprint Strength {
	tab event_name if event_type == "`event'"
}

********************************************************************************
* Table A2: summary statistics
********************************************************************************

*** all events
	tabstat norm_result Meet_tavg Meet_dewpoint wind Meet_preciptotal Meet_ozone Team_tavg travel_dist, s(N mean sd min max)
	tabstat norm_result, s(N mean sd min max) by(event_type)

*** endurance events 
	preserve
		keep if event_type == "Endurance"
		tabstat Meet_tavg Meet_RCP45temp Meet_RCP85temp Meet_dewpoint wind Meet_preciptotal Meet_ozone ///
			    Team_tavg Team_RCP45temp Team_RCP85temp travel_dist, s(N mean sd min max)
	restore

********************************************************************************
* Figure 1: nonlinear relation between temperature and performance by event type
********************************************************************************

foreach event in Endurance Sprint Strength {
	preserve
		keep if event_type == "`event'"
		reghdfe norm_result ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se) baselevels 
		outreg2 using Temperature_Effect_by_Event_Type.xls, excel ctitle("`event'") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 
	restore
}

********************************************************************************
* Table 1: adaptation from one-week exposure to warm temperatures
********************************************************************************

preserve
	keep if event_type == "Endurance"
	
	* (1) temperature difference
	reghdfe norm_result ib60.Meet_temp_bin c.temp_diff#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Temp_Diff.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (2) # days > 60 F in one week
	reghdfe norm_result ib60.Meet_temp_bin c.Team_hotter60#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Hotter_Days.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (3) mean training temperature
	reghdfe norm_result ib60.Meet_temp_bin c.Team_tavg#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Temp_Tavg.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (4) hottest 3rd of pre-exposure
	reghdfe norm_result ib60.Meet_temp_bin ib60.Meet_temp_bin#c.hot $weather_control if Team_tavg<50 | Team_tavg>60, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Hot_Dummy.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (5) # days > 60 F in one year
	reghdfe norm_result ib60.Meet_temp_bin ib60.Meet_temp_bin#c.Team_hotter60_365 $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Hotter_Days_One_Year.xls, excel dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) addtext(Hotter Days, 60F)

restore

********************************************************************************
* Figure 2: nonlinear relation between temperature and performance
********************************************************************************

preserve

	keep if event_type == "Endurance" 
	keep if Team_tavg<50 | Team_tavg>60

	* quadratic
	reghdfe norm_result c.Meet_tavg##c.Meet_tavg (c.Meet_tavg##c.Meet_tavg)#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se) 
	estimates save "Endurance_Quadratic", replace

	* cubic
	reghdfe norm_result c.Meet_tavg##c.Meet_tavg##c.Meet_tavg (c.Meet_tavg##c.Meet_tavg##c.Meet_tavg)#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	estimates save "Endurance_Cubic", replace

	* linear spline - 2 knots: 50F and 60F
	mkspline meet_linspl1 50 meet_linspl2 60 meet_linspl3  = Meet_tavg, di
	reghdfe norm_result meet_linspl* c.meet_linspl*#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	estimates save "Endurance_Linear_Spline", replace

	* cubic spline
	mkspline2 meet_cubspl= Meet_tavg, cubic di knots(49.6 56.7 63.8)
	reghdfe norm_result meet_cubspl* c.meet_cubspl*#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	estimates save "Endurance_Cubic_Spline", replace

restore

********************************************************************************
* Table A4: adaptation from one-week exposure by athlete tiers
********************************************************************************

forvalues i = 1/3 {
	preserve
		
		keep if event_type == "Endurance" & athleteTier == `i'
		
		* (1) temperature difference
		reghdfe norm_result ib60.Meet_temp_bin c.temp_diff#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Temp_Diff_Athlete_Tier.xls, excel dec(5) ctitle("Tier`i'") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (2) # days > 60 F in one week
		reghdfe norm_result ib60.Meet_temp_bin c.Team_hotter60#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Hotter_Days_Athlete_Tier.xls, excel dec(5) ctitle("Tier`i'") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (3) mean training temperature
		reghdfe norm_result ib60.Meet_temp_bin c.Team_tavg#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Temp_Tavg_Athlete_Tier.xls, excel dec(5) ctitle("Tier`i'") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (4) hottest 3rd of pre-exposure
		reghdfe norm_result ib60.Meet_temp_bin ib60.Meet_temp_bin#c.hot $weather_control if Team_tavg<50 | Team_tavg>60, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Hot_Dummy_Athlete_Tier.xls, excel dec(5) ctitle("Tier`i'") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	restore
}

********************************************************************************
* Table A5: acclimatization by week of competition season
********************************************************************************

preserve
	
	keep if event_type == "Endurance" & season_week <= 23
	
	* (1) temperature difference
	reghdfe norm_result ib60.Meet_temp_bin c.temp_diff#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Temp_Diff_Comp_Season.xls, excel dec(5) ctitle("<23") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (2) # days > 60 F in one week
	reghdfe norm_result ib60.Meet_temp_bin c.Team_hotter60#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Hotter_Days_Comp_Season.xls, excel dec(5) ctitle("<23") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (3) mean training temperature
	reghdfe norm_result ib60.Meet_temp_bin c.Team_tavg#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Temp_Tavg_Comp_Season.xls, excel dec(5) ctitle("<23") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (4) hottest 3rd of pre-exposure
	reghdfe norm_result ib60.Meet_temp_bin ib60.Meet_temp_bin#c.hot $weather_control if Team_tavg<50 | Team_tavg>60, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Hot_Dummy_Comp_Season.xls, excel dec(5) ctitle("<23") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

restore

preserve
	
	keep if event_type == "Endurance" & season_week > 23
	
	* (1) temperature difference
	reghdfe norm_result ib60.Meet_temp_bin c.temp_diff#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Temp_Diff_Comp_Season.xls, excel dec(5) ctitle(">23") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (2) # days > 60 F in one week
	reghdfe norm_result ib60.Meet_temp_bin c.Team_hotter60#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Hotter_Days_Comp_Season.xls, excel dec(5) ctitle(">23") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (3) mean training temperature
	reghdfe norm_result ib60.Meet_temp_bin c.Team_tavg#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Temp_Tavg_Comp_Season.xls, excel dec(5) ctitle(">23") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	* (4) hottest 3rd of pre-exposure
	reghdfe norm_result ib60.Meet_temp_bin ib60.Meet_temp_bin#c.hot $weather_control if Team_tavg<50 | Team_tavg>60, absorb($absorb_var) vce(cluster $cluster_se)
	outreg2 using Adapt_Hot_Dummy_Comp_Season.xls, excel dec(5) ctitle(">23") drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

restore

********************************************************************************
* Table A15: adaptation from one-week exposure in other events
********************************************************************************

foreach event in Sprint Strength {
	preserve
		keep if event_type == "`event'"
		
		* (1) temperature difference
		reghdfe norm_result ib60.Meet_temp_bin c.temp_diff#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Temp_Diff_Sprint_Strength.xls, excel ctitle(`event') dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (2) # days > 60 F in one week
		reghdfe norm_result ib60.Meet_temp_bin c.Team_hotter60#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Hotter_Days_Sprint_Strength.xls, excel ctitle(`event') dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (3) mean training temperature
		reghdfe norm_result ib60.Meet_temp_bin c.Team_tavg#ib60.Meet_temp_bin $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Temp_Tavg_Sprint_Strength.xls, excel ctitle(`event') dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

		* (4) hottest 3rd of pre-exposure
		reghdfe norm_result ib60.Meet_temp_bin ib60.Meet_temp_bin#c.hot $weather_control if Team_tavg<50 | Team_tavg>60, absorb($absorb_var) vce(cluster $cluster_se)
		outreg2 using Adapt_Hot_Dummy_Sprint_Strength.xls, excel ctitle(`event') dec(5) drop($weather_control) adds(Adjusted r2, e(r2_a), Number of Athletes, e(N_clust2)) 

	restore
}

********************************************************************************
* Figure A8: nonlinear relation between temperature and performance in other events
********************************************************************************

foreach event in Sprint Strength {
	preserve

		keep if event_type == "`event'" 
		keep if Team_tavg<50 | Team_tavg>60

		* quadratic
		reghdfe norm_result c.Meet_tavg##c.Meet_tavg (c.Meet_tavg##c.Meet_tavg)#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se) 
		estimates save "`event'_Quadratic", replace

		* cubic
		reghdfe norm_result c.Meet_tavg##c.Meet_tavg##c.Meet_tavg (c.Meet_tavg##c.Meet_tavg##c.Meet_tavg)#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		estimates save "`event'_Cubic", replace

		* linear spline - 2 knots: 50F and 60F
		mkspline meet_linspl1 50 meet_linspl2 60 meet_linspl3  = Meet_tavg, di
		reghdfe norm_result meet_linspl* c.meet_linspl*#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		estimates save "`event'_Linear_Spline", replace

		* cubic spline
		mkspline2 meet_cubspl= Meet_tavg, cubic di knots(49.6 56.7 63.8)
		reghdfe norm_result meet_cubspl* c.meet_cubspl*#c.hot $weather_control, absorb($absorb_var) vce(cluster $cluster_se)
		estimates save "`event'_Cubic_Spline", replace

	restore
}
